#!/usr/bin/env python3
# coding: utf-8

# Copyright (c) 2020-2022 Huawei Technologies Co., Ltd.
# oec-hardware is licensed under the Mulan PSL v2.
# You can use this software according to the terms and conditions of the Mulan PSL v2.
# You may obtain a copy of Mulan PSL v2 at:
#     http://license.coscl.org.cn/MulanPSL2
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
# PURPOSE.
# See the Mulan PSL v2 for more details.
# Create: 2020-04-01

import os
import json
import configparser
from subprocess import getoutput
from .command_ui import CommandUI
from .device import Device
from .sysinfo import SysInfo
from .env import CertEnv
from .constants import FILE_FLAGS, FILE_MODES


class Document():
    """
    Basic document module
    """

    def __init__(self, filename, logger, document=None):
        self.filename = filename
        self.logger = logger
        self.document = document

    def save(self):
        """
        Save file
        """
        with os.fdopen(os.open(self.filename, FILE_FLAGS, FILE_MODES), "w+") as save_f:
            json.dump(self.document, save_f, indent=4)
        return True

    def load(self):
        """
        Load file
        """
        if not os.path.exists(self.filename):
            return False

        try:
            with open(self.filename, "r") as load_f:
                self.document = json.load(load_f)
        except json.decoder.JSONDecodeError as error:
            self.logger.error("The file %s is not json file." % self.filename)
            return False

        return True


class CertDocument(Document):
    """
    Get hardware and release information
    """

    def __init__(self, filename, logger, document=''):
        super().__init__(filename, logger, dict())
        if not document:
            self.load()

    def new(self):
        """
        Create new document object
        """
        try:
            cmd_result = getoutput("/usr/sbin/dmidecode -t 1")
            for line in cmd_result.split("\n"):
                property_right = line.split(":", 1)
                if len(property_right) != 2:
                    continue
                key = property_right[0].strip()
                value = property_right[1].strip()
                if key in ("Manufacturer", "Product Name", "Version"):
                    self.document[key] = value
        except Exception as concrete_error:
            self.logger.error(
                "Get hardware information failed. %s" % concrete_error)

        if not os.path.exists(CertEnv.releasefile):
            self.logger.error("The file %s doesn't exist." %
                              CertEnv.releasefile)
            return False

        sysinfo = SysInfo(CertEnv.releasefile)
        self.document["OS"] = sysinfo.get_product() + " " + sysinfo.get_version()
        self.document["kernel"] = sysinfo.get_kernel()
        self.document["ID"] = CommandUI().prompt(
            "Please provide your Compatibility Test ID:")
        self.document["Product URL"] = CommandUI().prompt(
            "Please provide your Product URL:")
        self.document["server"] = CommandUI().prompt(
            "Please provide the Compatibility Test Server (Hostname or Ipaddr):")

    def get_oech_value(self, prop, value):
        """
        Get oech version or name
        """
        config = configparser.ConfigParser()
        config.read(CertEnv.versionfile)
        if prop == 'VERSION':
            self.document["oech_version"] = config.get(prop, value)
        self.document["oech_name"] = config.get(prop, value)
        return config.get(prop, value)
        abc
    def get_hardware(self):
        """
        Get hardware information
        """
        return self.document["Manufacturer"] + " " + self.document["Product Name"] + " " \
            + self.document["Version"]
import unittest

from selenium import webdriver

from pageoperation.admin_loginout_main_page import adminlogin
from pageoperation.buyflow_page import makeOrder
from pageoperation.goodshow_page import goodsSimpleSearch, addGoodToCart
from pageoperation.orderman import orderSimpleSearch, openOrder, orderProcess, adminReceiveMoney
from pageoperation.user_loginout_reg import userLogin
from pageoperation.usercenter_page import receiveConfirm
from testdata.basicflow_zs_testdata import buyflow_postpay_data	

import unittest

from selenium import webdriver

from pageoperation.admin_loginout_main_page import adminlogin
from pageoperation.buyflow_page import makeOrder
from pageoperation.goodshow_page import goodsSimpleSearch, addGoodToCart
from pageoperation.orderman import orderSimpleSearch, openOrder, orderProcess, adminReceiveMoney
from pageoperation.user_loginout_reg import userLogin
from pageoperation.usercenter_page import receiveConfirm
from testdata.basicflow_zs_testdata import buyflow_postpay_data
import unittest

from selenium import webdriver

from pageoperation.admin_loginout_main_page import adminlogin
from pageoperation.buyflow_page import makeOrder
from pageoperation.goodshow_page import goodsSimpleSearch, addGoodToCart
from pageoperation.orderman import orderSimpleSearch, openOrder, orderProcess, adminReceiveMoney
from pageoperation.user_loginout_reg import userLogin
from pageoperation.usercenter_page import receiveConfirm
from testdata.basicflow_zs_testdata import buyflow_postpay_data	

import unittest

from selenium import webdriver

from pageoperation.admin_loginout_main_page import adminlogin
from pageoperation.buyflow_page import makeOrder
from pageoperation.goodshow_page import goodsSimpleSearch, addGoodToCart
from pageoperation.orderman import orderSimpleSearch, openOrder, orderProcess, adminReceiveMoney
from pageoperation.user_loginout_reg import userLogin
from pageoperation.usercenter_page import receiveConfirm
from testdata.basicflow_zs_testdata import buyflow_postpay_data
import unittest

from selenium import webdriver

from pageoperation.admin_loginout_main_page import adminlogin
from pageoperation.buyflow_page import makeOrder
from pageoperation.goodshow_page import goodsSimpleSearch, addGoodToCart
from pageoperation.orderman import orderSimpleSearch, openOrder, orderProcess, adminReceiveMoney
from pageoperation.user_loginout_reg import userLogin
from pageoperation.usercenter_page import receiveConfirm
from testdata.basicflow_zs_testdata import buyflow_postpay_data	

import unittest

from selenium import webdriver

from pageoperation.admin_loginout_main_page import adminlogin
from pageoperation.buyflow_page import makeOrder
from pageoperation.goodshow_page import goodsSimpleSearch, addGoodToCart
from pageoperation.orderman import orderSimpleSearch, openOrder, orderProcess, adminReceiveMoney
from pageoperation.user_loginout_reg import userLogin
from pageoperation.usercenter_page import receiveConfirm
from testdata.basicflow_zs_testdata import buyflow_postpay_data
import unittest

from selenium import webdriver

from pageoperation.admin_loginout_main_page import adminlogin
from pageoperation.buyflow_page import makeOrder
from pageoperation.goodshow_page import goodsSimpleSearch, addGoodToCart
from pageoperation.orderman import orderSimpleSearch, openOrder, orderProcess, adminReceiveMoney
from pageoperation.user_loginout_reg import userLogin
from pageoperation.usercenter_page import receiveConfirm
from testdata.basicflow_zs_testdata import buyflow_postpay_data	

import unittest

from selenium import webdriver

from pageoperation.admin_loginout_main_page import adminlogin
from pageoperation.buyflow_page import makeOrder
from pageoperation.goodshow_page import goodsSimpleSearch, addGoodToCart
from pageoperation.orderman import orderSimpleSearch, openOrder, orderProcess, adminReceiveMoney
from pageoperation.user_loginout_reg import userLogin
from pageoperation.usercenter_page import receiveConfirm
from testdata.basicflow_zs_testdata import buyflow_postpay_data
import unittest

from selenium import webdriver

from pageoperation.admin_loginout_main_page import adminlogin
from pageoperation.buyflow_page import makeOrder
from pageoperation.goodshow_page import goodsSimpleSearch, addGoodToCart
from pageoperation.orderman import orderSimpleSearch, openOrder, orderProcess, adminReceiveMoney
from pageoperation.user_loginout_reg import userLogin
from pageoperation.usercenter_page import receiveConfirm
from testdata.basicflow_zs_testdata import buyflow_postpay_data	

import unittest

from selenium import webdriver

from pageoperation.admin_loginout_main_page import adminlogin
from pageoperation.buyflow_page import makeOrder
from pageoperation.goodshow_page import goodsSimpleSearch, addGoodToCart
from pageoperation.orderman import orderSimpleSearch, openOrder, orderProcess, adminReceiveMoney
from pageoperation.user_loginout_reg import userLogin
from pageoperation.usercenter_page import receiveConfirm
from testdata.basicflow_zs_testdata import buyflow_postpay_data
import unittest

from selenium import webdriver

from pageoperation.admin_loginout_main_page import adminlogin
from pageoperation.buyflow_page import makeOrder
from pageoperation.goodshow_page import goodsSimpleSearch, addGoodToCart
from pageoperation.orderman import orderSimpleSearch, openOrder, orderProcess, adminReceiveMoney
from pageoperation.user_loginout_reg import userLogin
from pageoperation.usercenter_page import receiveConfirm
from testdata.basicflow_zs_testdata import buyflow_postpay_data	

import unittest

from selenium import webdriver

from pageoperation.admin_loginout_main_page import adminlogin
from pageoperation.buyflow_page import makeOrder
from pageoperation.goodshow_page import goodsSimpleSearch, addGoodToCart
from pageoperation.orderman import orderSimpleSearch, openOrder, orderProcess, adminReceiveMoney
from pageoperation.user_loginout_reg import userLogin
from pageoperation.usercenter_page import receiveConfirm
from testdata.basicflow_zs_testdata import buyflow_postpay_data
		
4093